/*\
 *  Copyright (C) 2005  Anthony Liguori <anthony@codemonkey.ws>
 *
 *  Network Block Device
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; under version 2 of the License.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
\*/

#ifndef BITS_H
#define BITS_H

#include <stdint.h>
#include <endian.h>
#include <netinet/in.h>

#include <sys/types.h>
#include <unistd.h>
#include <stdbool.h>

#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <errno.h>
#include <string.h>
#include <stdio.h>

#if __BYTE_ORDER == __LITTLE_ENDIAN
#define be2cpu8(a)   (a)
#define be2cpu16(a)  (ntohs((a)))
#define be2cpu32(a)  (ntohl((a)))
#define be2cpu64(a)  \
(((uint64_t)(htonl((a) & 0xFFFFFFFF)) << 32) | htonl(((a) >> 32) & 0xFFFFFFFF))
#elif __BYTE_ORDER == __BIG_ENDIAN
#define be2cpu8(a)   (a)
#define be2cpu16(a)  (a)
#define be2cpu32(a)  (a)
#define be2cpu64(a)  (a)
#else
#error Byte order not defined
#endif

#define _v8(d, o)  ((uint8_t)((d)[(o)]))
#define _v16(d, o) ((uint16_t)((_v8((d), (o) + 1) << 8)   | _v8((d), (o))))
#define _v32(d, o) ((uint32_t)((_v16((d), (o) + 2) << 16) | _v16((d), (o))))
#define _v64(d, o) ((((uint64_t)_v32((d), (o) + 4)) << 32) | _v32((d), (o)))

#define val8(d, o)  be2cpu8(_v8(d, o))
#define val16(d, o) be2cpu16(_v16(d, o))
#define val32(d, o) be2cpu32(_v32(d, o))
#define val64(d, o) be2cpu64(_v64(d, o))

#define sval8(d, o, v)                                                        \
do {                                                                          \
	((d)[(o)] = (v));                                                     \
} while (0)
#define sval16(d, o, v)                                                       \
do {                                                                          \
	sval8((d), (o), ((v) >> 8) & 0xFF);                                   \
	sval8((d), (o) + 1, (v) & 0xFF);                                      \
} while (0)
#define sval32(d, o, v)                                                       \
do {                                                                          \
	sval16((d), (o), ((v) >> 16) & 0xFFFF);                               \
	sval16((d), (o) + 2, (v) & 0xFFFF);                                   \
} while (0)
#define sval64(d, o, v)                                                       \
do {                                                                          \
	sval32((d), (o), (((uint64_t)v) >> 32) & 0xFFFFFFFF);                 \
	sval32((d), (o) + 4, (v) & 0xFFFFFFFF);                               \
} while (0)

#define LOG(msg, ...) do {                                                    \
	fprintf(stderr, "%s:%s():L%d: " msg "\n",                             \
		__FILE__, __FUNCTION__, __LINE__, ## __VA_ARGS__);            \
} while(0)

extern int verbose;
#define TRACE(msg, ...) do {                                                  \
        if (verbose) LOG(msg, ## __VA_ARGS__);                                \
} while(0)

#define read_sync(fd, buffer, size) wr_sync(fd, buffer, size, true)
#define write_sync(fd, buffer, size) wr_sync(fd, buffer, size, false)

int tcp_socket_outgoing(const char *address, uint16_t port);
int tcp_socket_incoming(const char *address, uint16_t port);
size_t wr_sync(int fd, void *buffer, size_t size, bool do_read);

#endif
